package run.core;

import java.util.ArrayList;
import java.util.List;

import run.Cell;
import run.Facet;

/*
	ADD takes in n > 0 arguments and adds them. The individual
	input facets of ADD cannot be accessed individually, due to the nature of the cell.
		<(*) ADD (.out)>
 		*: any number greater than 0 of INT inputs
		out: an INT
*/

public class ADD extends Cell {
	private Facet in, out;
	
	public ADD(String labelName) {
		super("ADD", labelName);
		in = new Facet(".in", this, INPUT_FACET);
		out = new Facet(".out", this, OUTPUT_FACET);
		out.addSource(in);
	}
	
	@Override
	public List<Facet> stimulate(Facet stimmer) {
		List<Object> summands = in.dispatchOutboundEntire();
		
		// CHECK FOR ALLNILS -- THEN MAKE NIL
		Integer sum = 0;
		boolean allNil = true;
		for (Object o : summands) {
			if (!(o instanceof NIL)) {
				allNil = false;
				Integer i = (Integer) o;
				sum += i;
			}
		}
		
		List<Object> sums = new ArrayList<Object>();
		
		if (allNil) {
			sums.add(new NIL());
		}
		else {
			sums.add(sum);
		}
		
		out.receive(in, sums);
		
		ArrayList<Facet> doneFacets = new ArrayList<Facet>();
		doneFacets.add(out);
		
		return doneFacets;
	}
	
	public List<Facet> getFacets()
	{
		ArrayList<Facet> temp = new ArrayList<Facet>();
		temp.add(in);
		temp.add(out);
		return temp;
	}

	//Dot printout method
	public String toDot(String prefix)
	{
		String returnString = "";
		returnString += "subgraph " + "cluster_" + cellTypeName + hashCode() + "{\n";
		returnString += "label = \"" + cellTypeName + "\";\n";
		
		//Print my facets
		returnString +=  prefix + cellTypeName + hashCode() + "in [label=\"in\"];\n";
		returnString += prefix + cellTypeName + hashCode() + "out [label=\"out\"];\n";
		
		returnString += "}\n";
		return returnString;
	}
	
}
